using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace UFrame
{
    public class UIEventListener : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerEnterHandler,
        IPointerExitHandler, IPointerUpHandler
    {

        public Action<GameObject, PointerEventData> OnClick;
        public Action<GameObject, PointerEventData> OnDown;
        public Action<GameObject, PointerEventData> OnEnter;
        public Action<GameObject, PointerEventData> OnExit;
        public Action<GameObject, PointerEventData> OnUp;

        public float durThreshold = 1.0f; //长按阈值
        public float durFrequency = 0.3f;

        public UnityEvent onLongPress = new UnityEvent();
        public UnityEvent onPress = new UnityEvent();
        public UnityEvent onLongDurPress = new UnityEvent();

        private bool isPointerDown = false;
        private bool isLongPress = false;
        private float timePressStart;

        public static UIEventListener Get(GameObject go)
        {
            var com = go.GetComponent<UIEventListener>();
            if (com == null)
            {
                com = go.AddComponent<UIEventListener>();
            }

            return com;
        }

        private void Update()
        {
            if (isPointerDown)
            {
                //长按一直触发
                onPress.Invoke();

                //长按多少秒之后触发一次
                if (!isLongPress)
                {
                    if (Time.time - timePressStart > durThreshold)
                    {
                        isLongPress = true;
                        onLongPress.Invoke();
                        timePressStart = Time.time;
                    }
                }
                else
                {
                    //长按多少秒之后触发一次，每隔多少秒触发一次
                    if (Time.time - timePressStart > durFrequency)
                    {
                        onLongDurPress.Invoke();
                        timePressStart = Time.time;
                    }
                }
            }
        }


        public void OnPointerClick(PointerEventData eventData)
        {
            if (OnClick != null)
            {
                OnClick(gameObject, eventData);
            }
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            isPointerDown = true;
            timePressStart = Time.time;
            isLongPress = false;
            if (OnDown != null)
            {
                OnDown(gameObject, eventData);
            }
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            if (OnEnter != null)
            {
                OnEnter(gameObject, eventData);
            }
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            isPointerDown = false;
            if (OnExit != null)
            {
                OnExit(gameObject, eventData);
            }
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            isPointerDown = false;
            if (OnUp != null)
            {
                OnUp(gameObject, eventData);
            }
        }


        public static void ToggleListener(GameObject go, Action<GameObject, bool> call)
        {
            var toggle = go.GetComponent<Toggle>();
            if (toggle == null)
            {
                Debug.LogError(string.Format("{0} hasn't found component 'Toggle'", go.name));
                return;
            }

            toggle.onValueChanged.AddListener((r) => { call(go, r); });
        }
    }
}